/*
DATE: 2021/12/07
AUTHOR: chenyihui
Desc:   消息传递对象

*/
package dingtalk

// ========== text msg ========== //
type textMsg struct {
	MsgType msgType `json:"msgtype,omitempty"`
	Text    textMdl `json:"text,omitempty"`
	At      atMdl   `json:"at,omitempty"`
}

func newTextMsg(content string, opts ...atOption) *textMsg {
	msg := &textMsg{MsgType: Text, Text: textMdl{Content: content}}
	for _, opt := range opts {
		opt.apply(&msg.At)
	}
	return msg
}

// ========== link msg ========== //
type linkMsg struct {
	MsgType msgType `json:"msgtype,omitempty"`
	Link    linkMdl `json:"link,omitempty"`
	At      atMdl   `json:"at,omitempty"`
}

func newLinkMsg(title, text, picUrl, msgUrl string) *linkMsg {
	msg := &linkMsg{
		MsgType: Link,
		Link: linkMdl{
			Text:       text,
			Title:      title,
			PicUrl:     picUrl,
			MessageUrl: msgUrl,
		},
	}
	return msg
}

// ========== markdown msg ========== //
type markdownMsg struct {
	MsgType  msgType     `json:"msgtype,omitempty"`
	Markdown markDownMdl `json:"markdown,omitempty"`
	At       atMdl       `json:"at,omitempty"`
}

func newMarkdownMsg(title, text string, opts ...atOption) *markdownMsg {
	msg := &markdownMsg{
		MsgType: Markdown,
		Markdown: markDownMdl{
			Title: title,
			Text:  text,
		},
	}
	for _, opt := range opts {
		opt.apply(&msg.At)
	}
	return msg
}

// ========== action card  msg ========== //
type actionCardMsg struct {
	MsgType    msgType       `json:"msgtype,omitempty"`
	ActionCard actionCardMdl `json:"actionCard"`
}

func newActionCardMsg(title, text string, opts ...acOption) *actionCardMsg {
	msg := &actionCardMsg{
		MsgType: ActionCard,
		ActionCard: actionCardMdl{
			Title:          title,
			Text:           text,
			BtnOrientation: vertical,
		},
	}
	for _, opt := range opts {
		opt.apply(&msg.ActionCard)
	}
	return msg
}

// ========== feed card msg ========== //
type feedCardMsg struct {
	MsgType  msgType     `json:"msgtype,omitempty"`
	FeedCard feedCardMdl `json:"feedCard,omitempty"`
}

func newFeedCardMsg(links []feedCardLinkMdl) *feedCardMsg {
	msg := &feedCardMsg{
		MsgType:  FeedCard,
		FeedCard: feedCardMdl{Links: links},
	}
	return msg
}
